﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.City6473.Web.Data.Repositories;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace SpaceNeedle.City6473.WebSite
{
    public static class AdminConfig
    {
        public static void Setup()
        {
            try
            {
                UsersAuthenticationRepository repository = new UsersAuthenticationRepository();

                string adminName = ConfigurationManager.AppSettings["adminName"];
                string adminEmail = ConfigurationManager.AppSettings["adminEmail"];
                string adminFirstPassword = ConfigurationManager.AppSettings["adminFirstPassword"];
                string adminRole = ConfigurationManager.AppSettings["adminRole"];

                var admin = repository.GetAll().Where(x => x.Name == adminName && x.Email == adminEmail).SingleOrDefault();

                if (admin == null)
                {
                    admin = new UsersAuthentication();
                    admin.Id = Guid.NewGuid();
                    admin.Name = adminName;
                    admin.Email = adminEmail;
                    admin.Password = SpaceNeedle.City6473.Web.Data.Security.Encode(adminFirstPassword);
                    admin.IsActive = true;
                    admin.Roles = adminRole;

                    repository.Insert(admin);
                }
            }
            catch { }
        }
    }
}